package com.leon.learnenginespace.drools.utils;

import com.leon.learnenginespace.drools.dao.ProSaleRuleMapper;
import com.leon.learnenginespace.drools.pojo.OrganPojo;
import com.leon.learnenginespace.drools.pojo.TDrlProSaleRule;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

/**
 * 自动生成规则
 *
 * @author lhy
 * @date 2023年7月14日
 */
@Data
@Component
public class AutoCreateRuleUtil {

    @Autowired
    ProSaleRuleMapper proSaleRuleMapper;
    @Autowired
    RuleUtils ruleUtils;

    /**
     * 根据规则模板自动生成规则内容
     *
     * @param code
     * @param organPojo
     * @param year
     * @param tDrlProSaleRule
     * @return
     */
    public String createRule(String code, OrganPojo organPojo, String year, TDrlProSaleRule tDrlProSaleRule) {
        String organId = organPojo.getOrganId();
        String organName = organPojo.getOrganName();
        String riskCode = tDrlProSaleRule.getRiskCode();
        String effectFlag = tDrlProSaleRule.getEffectFlag();
        String startDate = tDrlProSaleRule.getStartDate();
        String endDate = tDrlProSaleRule.getEndDate();
        String templateId = tDrlProSaleRule.getTemplateId();
        String pattern = proSaleRuleMapper.queryRuleTemplate(templateId);
        //0-规则编码、1-产品编码、2-屏蔽机构编码、3-交费期间、4、屏蔽机构名称、5-生效标记、6-生效开始日期、7-生效结束日期
        String rule = MessageFormat.format(pattern, code, riskCode, organId, year, organName, effectFlag, startDate, endDate);

        return rule;
    }
}
